"use strict";

var fs = require('fs');
var path = require('path');

var readDir = function (dir) {
    return new Promise(function (resolve, reject) {
        fs.readdir(dir, function (err, files) {
            if (err) reject(err);
            resolve(files);
        })
    })
}

var stat = function (path) {
    return new Promise(function (resolve, reject) {
        fs.stat(path, function (err, stat) {
            if (err) reject(err)
            resolve(stat)
        })
    })
}

function findLargest(dir) {
    return readDir(dir)
        .then(function (files) {
            let promises = files.map(file => stat(path.join(dir, file)));
            return Promise.all(promises)
                .then(function (stats) {
                    return {stats, files}
                })
        })
        .then(data => {
            let largest = data.stats
                .filter(function (stat) {
                    return stat.isFile()
                })
                .reduce((prev, next) => {
                    if (prev.size > next.size) return prev;
                    return next;
                })
            return data.files[data.stats.indexOf(largest)];
        })
}

findLargest('./')
    .then(function (filename) {
        console.log('Promise demo largest file was:', filename);
    })
    .catch(function () {
        console.log(error);
    })














